package namespace.jcu.mobile.library.system;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.TreeSet;

public class ContactUsActivity extends ListActivity {

    private MyCustomAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //creating MyCustomerAdapter variable
        mAdapter = new MyCustomAdapter();
        
        //simply call addSeparatorItem and addItem method to create a separator view and normal item view respectively
        //where parameter is the text you want to display
        mAdapter.addSeparatorItem("   Email");
        mAdapter.addItem("library@my.jcu.edu.sg");

        mAdapter.addSeparatorItem("   Hotline");
        mAdapter.addItem("+65 5522 2111");
        
        mAdapter.addSeparatorItem("   Fax");
        mAdapter.addItem("+65 6455 2833");
        
        mAdapter.addSeparatorItem("   Mailing Address");
        mAdapter.addItem("600 Upper Thomson Road, Singapore 574421");
        
        setListAdapter(mAdapter);
    }

    
    //inner class that extends BaseAdapter class to construct a view for each item
    private class MyCustomAdapter extends BaseAdapter {

        private static final int TYPE_ITEM = 0;
        private static final int TYPE_SEPARATOR = 1;
        private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

        private ArrayList<String> mData = new ArrayList<String>();
        private LayoutInflater mInflater;

        private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

        
        //constructors to get layoutinflater
        public MyCustomAdapter() {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        //add text into list item view and display
        public void addItem(final String item) {
            mData.add(item);
            notifyDataSetChanged();
        }

        //add text into separator view and display
        public void addSeparatorItem(final String item) {
            mData.add(item);
            // save separator position
            mSeparatorsSet.add(mData.size() - 1);
            notifyDataSetChanged();
        }

        //get the type of item view whether it's separator or else
        @Override
        public int getItemViewType(int position) {
            return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
        }

        //get the count of item view type
        public int getViewTypeCount() {
            return TYPE_MAX_COUNT;
        }

        //get overall adapter size
        public int getCount() {
            return mData.size();
        }

        //get the position of selected item
        public String getItem(int position) {
            return mData.get(position);
        }

        //get id of selected item
        public long getItemId(int position) {
            return position;
        }

        
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            int type = getItemViewType(position);
            
            //check result at logcat whether output is correct or not
            System.out.println("getView " + position + " " + convertView + " type = " + type);
            
            if (convertView == null) {
                holder = new ViewHolder();
                switch (type) {
                
                //setting up Normal item view when ITEM TYPE is 0 , i.e normal view
                    case TYPE_ITEM:
                        convertView = mInflater.inflate(R.layout.item_description, null);
                        holder.textView = (TextView)convertView.findViewById(R.id.text);
                        break;
                //setting up Separator view when ITEM TYPE is 1, i.e separator view
                    case TYPE_SEPARATOR:
                        convertView = mInflater.inflate(R.layout.item_separator, null);
                        holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                        break;
                }
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
            
            //input the text that user input into textview chosen above, according to item_type
            holder.textView.setText(mData.get(position));
            return convertView;
        }

    }

    public static class ViewHolder {
        public TextView textView;
    }
}